/*
 **********************************************************************************
 * Copyright www.ebidding.com.cn 2017年09月07日 Authors: 符强年 <fqn@ebidding.com.cn>*
 **********************************************************************************
 */
package com.example.demo.common.msg.vo;
import com.example.demo.common.exception.EbsException;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 作者: 符强年
 * 创建时间: 2017年09月07日
 * 功能描述:
 * 版本: 1.0.0
 */

public class MessageVo {

    private String accessToken;

    private String msgNodeCode;

    /** 待办url */
    private String todoUrl;

    /** 业务CODE，待办专用 */
    private String businessCode;

    /** 业务id，待办专用 */
    private Integer businessId;


    private Map<String,String> attrs = new HashedMap();

    private Set<EbsMessageReceiver> receivers = new HashSet<>();



    public void addEmail(String val) {

        if(StringUtils.isEmpty(val)) return;
        receivers.stream().filter(receiver -> val.equals(receiver.getEmail())).findAny().ifPresent((e)->{
            return ;
        });

        EbsMessageReceiver r = new EbsMessageReceiver();
        r.setEmail(val);
        receivers.add(r);
    }

    public void addMobile(String val) {
        if(StringUtils.isEmpty(val)) return;
        receivers.stream().filter(receiver -> val.equals(receiver.getMobile())).findAny().ifPresent((e)->{
            return ;
        });

        EbsMessageReceiver r = new EbsMessageReceiver();
        r.setMobile(val);
        receivers.add(r);
    }

    public void addUserId(Integer val) {
        if(null == val) return;
        receivers.stream().filter(receiver -> val.equals(receiver.getUserId())).findAny().ifPresent((e)->{
            return ;
        });

        EbsMessageReceiver r = new EbsMessageReceiver();
        r.setUserId(val);
        receivers.add(r);
    }

    public void addReceiver(String email,String mobile,Integer userId){
        if(userId == null && StringUtils.isEmpty(email) && StringUtils.isEmpty(mobile)){
            throw new EbsException("消息接收人信息不能都为空");
        }
        EbsMessageReceiver receiver = new EbsMessageReceiver(email,mobile,userId);
        receivers.add(receiver);
    }


    public void setAttributes(Map<String, String> attrs) {
        this.attrs = attrs;
    }

    public void addAttribute(String name, String value) {
        this.attrs.put(name,value);
    }

    public Set<EbsMessageReceiver> getReceivers() {
        return receivers;
    }

    public void setReceivers(Set<EbsMessageReceiver> receivers) {
        this.receivers = receivers;
    }

    public String getMsgNodeCode() {
        return msgNodeCode;
    }

    public void setMsgNodeCode(String msgNodeCode) {
        this.msgNodeCode = msgNodeCode;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public Map<String, String> getAttrs() {
        return attrs;
    }

    public String getTodoUrl() {
        return todoUrl;
    }

    public void setTodoUrl(String todoUrl) {
        this.todoUrl = todoUrl;
        this.attrs.putIfAbsent("todoUrl",todoUrl);
    }

    public String getBusinessCode() {
        return businessCode;
    }

    public void setBusinessCode(String businessCode) {
        this.businessCode = businessCode;
    }

    public Integer getBusinessId() {
        return businessId;
    }

    public void setBusinessId(Integer businessId) {
        this.businessId = businessId;
    }

    public static void main(String args[]){
        MessageVo v = new MessageVo();
        v.addReceiver("a@3f.c",null,1);
        v.addReceiver("a@3f.c",null,1);
        v.addReceiver("a@23f.c",null,1);
        System.out.printf("size:" +v.receivers.size());
        System.out.println("3".equals(MessageType.TODO.getValue()));
        MessageType m = MessageType.instanceOf("1");
        System.out.println(m);
    }
}
